package com.nbst.controller.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nbst.dao.mapper.mes.DepartmentMapper;
import com.nbst.model.base.Base;
import com.nbst.model.base.Department;
import com.nbst.model.base.User;
import com.nbst.service.base.IUserService;

/**
 * @ClassName: DepartmentController
 * @Description:部门
 * @author King
 * @date 2018年11月13日
 * 
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
	@Autowired
	IUserService userService;
	
	@Autowired
	DepartmentMapper departmentMapper;

	/**
	 * 获取部门树
	 * 
	 * @return Object
	 * @author King
	 * @date 2018年11月13日
	 */
	@GetMapping("/departmentTree.action")
	public Object getDepartmentTree() {
		Map<String,Object> map = null;
		List<Base> list = userService.getDepartmentUserTree(map);
		List<Base> dept = new ArrayList<Base>();
		for (Base base : list) {
			if (!"user".equals(base.getFlag())) {
				dept.add(base);
			}
		}
		return dept;
	}
	
	@GetMapping("/getbmList.action")
	public Object getbmList() {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		List<Department> list = new ArrayList<Department>();
		if("admin".equals(user.getAccount())||"system".equals(user.getAccount())) {
			Map<String,Object> map = new HashMap<>(8);
			map.put("fDProperty", 1070);
			list = departmentMapper.findByCondition(map);
		}else {
			list = departmentMapper.findbyUId(user.getId());
		}
		return list;
	}
	
	/**
	 * 获取部门名称与id
	 * @return
	 * Object 
	 * @author King
	 * @date 2018年11月15日
	 */
	@GetMapping("/getDepNameAndId.action")
	public Object getDepNameAndId() {
		List list = new ArrayList<>();
		try {
			list = departmentMapper.getKeyAndValueList();
		} catch (Exception e) {
			System.out.println(e);
		}
				return list;
		
	}

}
